#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// 定义线程锁
pthread_mutex_t mutex;
pthread_mutex_t mutex1;

// 添加我的界面
#include "mywin.h"

#define DISP_BUF_SIZE (480 * 1024)
int fd;
void my_lvgl_init()
{
    /*lvgl初始化*/
    lv_init();
    /*输出设备初始化及注册*/
    fbdev_init();
    /*A small buffer for LittlevGL to draw the screen's content*/
    static lv_color_t buf[DISP_BUF_SIZE];
    /*Initialize a descriptor for the buffer*/
    static lv_disp_draw_buf_t disp_buf;
    lv_disp_draw_buf_init(&disp_buf, buf, NULL, DISP_BUF_SIZE);
    /*Initialize and register a display driver*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &disp_buf;
    disp_drv.flush_cb = fbdev_flush;
    disp_drv.hor_res  = 800;
    disp_drv.ver_res  = 480;
    lv_disp_drv_register(&disp_drv);

    // 输入设备初始化及注册
    evdev_init();
    static lv_indev_drv_t indev_drv_1;
    lv_indev_drv_init(&indev_drv_1); /*Basic initialization*/
    indev_drv_1.type = LV_INDEV_TYPE_POINTER;
    /*This function will be called periodically (by the library) to get the mouse position and state*/
    indev_drv_1.read_cb      = evdev_read;
    lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv_1);
}
int main(void)
{
    my_lvgl_init(); // 官方示例lvgl的初始化
    /***************************从👇开始写入自己的字库代码*******************************/
    fd = open("/pipe",O_RDWR);
    if (fd < 0)
    {
        perror("fail:");
        return -1;
    }
    
    pthread_mutex_init(&mutex, NULL); // 初始化线程锁
    pthread_mutex_init(&mutex1, NULL); // 初始化线程锁

    show_main_win();                  // 显示初始界面
    while(1) {
        pthread_mutex_lock(&mutex);   // 上锁
        lv_timer_handler();           // 事务处理
        pthread_mutex_unlock(&mutex); // 解锁
        lv_tick_inc(5);               // 节拍累计
        usleep(5000);
    }
    return 0;
}
